//package com.he.hsdyc.config.security;
//
//import com.he.hsdyc.service.MemberService;
//import com.he.hsdyc.service.MyUserDetailService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
//import org.springframework.stereotype.Component;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
//@Component
//public class AuthenticationTokenFilter extends OncePerRequestFilter {
//
//
//    @Autowired
//    private MyUserDetailService userService; //用户信息service
//
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
//        String authHeader = request.getHeader("token");//获取header中的验证信息
//
//        if (authHeader != null && authHeader.startsWith("Bearer ")) {
//            final String authToken = authHeader.substring("Bearer ".length());
//
//            String username = JwtTokenUtil.parseToken(authToken, "_secret"); //从token中获取用户信息，jwtUtils自定义的token加解密方式
//
//            if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
//                UserDetails userDetails = userService.loadUserByUsername(username);//根据用户名获取用户对象
//
//                if (userDetails != null) {
//                    UsernamePasswordAuthenticationToken authentication =
//                            new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
//                    authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
//                    //设置为已登录
//                    SecurityContextHolder.getContext().setAuthentication(authentication);
//                }
//            }
//        }
//
//        chain.doFilter(request, response);
//    }
//}